package com.ruoyi.standingbook.v2toilet.report;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;

@Slf4j
public class CellStyleHander implements CellWriteHandler {
    static final String V1 = "1";
    static final String V2 = "2";
    static final String V3 = "3";
    private int rowMin;
    private int rowMax;
    private int colMin;
    private int colMax;


    public CellStyleHander() {
        this.rowMin = 1;
        this.rowMax = 99;
        this.colMin = 3;
        this.colMax = 99;
    }

    /**
     * @param rowMin 0 开始 excel中数字-1
     * @param rowMax 0 开始 excel中数字-1
     * @param colMin 0 开始 excel中数字-1
     * @param colMax 0 开始 excel中数字-1
     */
    public CellStyleHander(int rowMin, int rowMax, int colMin, int colMax) {
        this.colMin = colMin;
        this.colMax = colMax;
        this.rowMin = rowMin;
        this.rowMax = rowMax;
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        int rowIdx = cell.getRowIndex();
        int colIdx = cell.getColumnIndex();

        if (rowIdx < rowMin || rowIdx > rowMax) {
            return;
        }
        if (colIdx < colMin || colIdx > colMax)
            return;
        String cellValue = cell.getStringCellValue();
        //        log.info("row:{}; col:{} ;value:{}", rowIdx, colIdx, cell);
        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
        WriteCellData<?> cellData = context.getFirstCellData();
        WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
        if (V1.equalsIgnoreCase(cellValue)) {
            cell.setCellValue("异常");
            writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        } else if (V2.equalsIgnoreCase(cellValue)) {
            cell.setCellValue("合格");
            writeCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        } else if (V3.equalsIgnoreCase(cellValue)) {
            cell.setCellValue("合格");
        } else {
            cell.setCellValue("");
        }
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
//        }
    }
}
